package main

import (
	"cli"
	"github.com/labstack/gommon/log"
	"os"
	"controller"
	"config"
	"github.com/labstack/echo"
	"model"
)

var app = cli.NewApp()

func init() {
	app.Version = "0.0.1"
	app.Name = "marry"
	app.Authors = []cli.Author{
		{
			Name:  "张梦金",
			Email: "735416909@qq.com",
		}, {
			Name:  "罗志超",
			Email: "unknown@qq.com",
		},
	}
	app.Commands = []cli.Command{
		{
			Name:      "web",
			ShortName: "w",
			Action:    web,
			Before: webInit,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "config,c",
					Value: "./config/config.yaml",
				},
			},
		},
	}
	app.Usage = "marry web"
	app.UsageText = "start web server"
}

func main() {
	err := app.Run(os.Args)
	if err != nil {
		log.Print(err)
		os.Exit(1)
	}
}


func web(ctx *cli.Context) {
	e := echo.New()
	g := e.Group("/api")
	controller.Route(g)
	if err := e.Start(config.Config.Web.Port) ; err != nil {
		log.Errorf("web server start error :%+v",err)
		os.Exit(1)
	}
}

func webInit(ctx *cli.Context) error {
	err := config.Init(ctx)
	if err != nil {
		return err
	}
	err = model.InitDb(config.Config.Mysql)
	if err != nil {
		return err
	}
	return nil
}
